//
//  CZComposeBottomView.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//创建枚举
enum CZComposeBottomViewType: Int{
    //图片
   case picture = 0
    //@
   case mention = 1
    //#
   case trend = 2
    //笑脸
   case emoticon = 3
    //+
   case add = 4
    
}


class CZComposeBottomView: UIView {

    
    //声明一个闭包
    var closure:((type:CZComposeBottomViewType)->())?
    
    var emoticonButton:UIButton?
    //设置一个属性供外界赋值
    var isEmoticon:Bool = false{
        
        didSet{
            /*
             true  代表当前为 代表显示为表情键盘  -> 显示为 键盘图片
             false 代表当前为 代表显示为系统键盘  -> 显示为 笑脸图片
             */
            var imageName:String?
            if isEmoticon {
               //显示键盘图片compose_keyboardbutton_background_highlighted
                
                imageName = "compose_keyboardbutton_background"
            }else{
                //显示笑脸图片
                imageName = "compose_emoticonbutton_background"
            }
            
            emoticonButton?.setImage(UIImage(named: imageName!), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        }
 
    }
 
 
    //点击事件的监听
    @objc private func btnClick(button:UIButton){
        //tag值转成枚举值
        closure?(type:CZComposeBottomViewType(rawValue: button.tag)!)
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //MARK: -设置UI
    func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        //添加按钮
        let pictureButton = addChildButtons("compose_toolbar_picture",type: .picture)
        let mentionButton = addChildButtons("compose_mentionbutton_background",type: .mention)
        let trendButton = addChildButtons("compose_trendbutton_background",type: .trend)
        emoticonButton = addChildButtons("compose_emoticonbutton_background",type: .emoticon)
        let addButton = addChildButtons("compose_add_background",type: .add)
        
        //设置约束
        
        pictureButton.snp_makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        
        emoticonButton!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        
        addButton.snp_makeConstraints { (make) in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
        
    }
    
    
    
    
    //MARK: -抽取的添加底部按钮的方法
    func addChildButtons(imageName:String,type:CZComposeBottomViewType)->UIButton{
        
        let btn = UIButton()
         // 设置tag //type.rawValue 通过枚举名 得到对应的枚举值
        btn.tag = type.rawValue
        //添加点击事件
        btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到底部view中
        addSubview(btn)
        return btn
    }
    
    
}











